<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Three 创建场景</title>
  <style>
    .three-show {
      width: 500px;
      height: 500px;
      border: 1px solid red;
    }
  </style>
</head>
<body>

  <div class="three-show"></div>

  <script src="../js/three.js"></script>
  <script>
    const oBox = document.getElementsByClassName('three-show')[0]

    /** 
     * 场景、相机、渲染器
     *  */
    const scene = new THREE.Scene()
    const camera = new THREE.PerspectiveCamera(75, oBox.clientWidth / oBox.clientHeight, 0.1, 1000)
    const renderer = new THREE.WebGLRenderer()

    renderer.setSize(oBox.clientWidth, oBox.clientHeight)

    oBox.appendChild(renderer.domElement)

    /** 绘制正方体 */
    const geometry = new THREE.BoxGeometry(1, 1, 1)
    const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })
    const cube = new THREE.Mesh( geometry, material )
    scene.add(cube) // 默认添加到 (0,0,0)

    camera.position.z = 5

    // 渲染
    function animate() {
      requestAnimationFrame(animate)
      moveBox()
      renderer.render(scene, camera)
    }

    function moveBox() {
      cube.rotation.x += 0.01
      cube.rotation.y += 0.01
    }

    function init() {
      /** 兼容性检测 */
      if(WebGL.isWebGLAvailable()) {
        animate()
      } else {
        const warning = WebGL.getWebGLErrorMessage()
        oBox.appendChild(warning)
      }
    }
  </script>
</body>
</html>